import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_niandaxue/config/device.dart';
import 'package:flutter_niandaxue/model/user_info.dart';
import 'package:flutter_niandaxue/pages/login_page.dart';
import 'package:flutter_niandaxue/pages/member/set_up_page.dart';
import 'package:flutter_niandaxue/service/http_service.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:flutter_swiper/flutter_swiper.dart';
import 'package:flutter_niandaxue/config/index.dart';

class MemberPage extends StatefulWidget{
  @override
  _MemberPageState createState() => _MemberPageState();

}

class _MemberPageState extends State<MemberPage>{
  UserAppInfo _userInfo = new UserAppInfo();
  String _nickName = "---";
  String _phoneNum = "---";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    _getUserInfo();
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Scaffold(
      appBar: AppBar(),
      body:Center(
          child: Column(
            children: <Widget>[
              // 1 个人信息部分
              Container(
                margin: EdgeInsets.only(left: 10, right: 10, top: 20),
                height: ScreenUtil().setHeight(150),
                decoration: new BoxDecoration( // 边框设置
                  color: Colors.white,
                  border: new Border.all(width: 1, color: Colors.white),
                  //设置四周圆角 角度
                  borderRadius: BorderRadius.all(Radius.circular(10.0)),
                ),
                child: Row(
                  children: <Widget>[
                    _myHeadPicture(),// 头像
                    _baseInfo(), // 基本信息
                  ],
                ) ,
              ),
              Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(left: 10, right: 10, top: 20),
                height: ScreenUtil().setHeight(80),
                decoration: new BoxDecoration( // 边框设置
                  color: Colors.white,
                  border: new Border.all(width: 1, color: Colors.white),
                  //设置四周圆角 角度
                  borderRadius: BorderRadius.all(Radius.circular(10.0)),
                ),
                child: ListTile(
                  title: Text(KString.setUp),
                  trailing: Icon(Icons.keyboard_arrow_right, color: Colors.grey, size: 40.0),
                  onTap: (){
                    print("跳转到版本信息页面");
                    Navigator.push(context, MaterialPageRoute(
                        builder:(context){
                          return new SetUpPage(userInfo: _userInfo,);
                        }
                    ));
                  },
                ),
              )
            ],
          )
      ),
    );
  }

  // 我的头像
  Widget _myHeadPicture(){
    return Container(
      child: IconButton(
        icon: Container(
          // 设置图片为圆形
//          child: ClipOval(
//            child: Image.asset(KImage.geren_2,
//              fit: BoxFit.cover,
//              height: 140,
//              width: 140,
//            ),
//          ),
          child: ClipRRect(
            borderRadius: BorderRadius.circular(8.0),
            child: Image.asset(KImage.geren_2,
              fit: BoxFit.cover,
              height: 140,
              width: 140,
            ),
          ),

        ),
        iconSize: 80.0,
        onPressed: (){
          print("更换头像图片");
        },
      ),
    );
  }

  Widget _baseInfo(){
    return Container(
        margin: EdgeInsets.only(left: 20.0, top: 20, ),
        alignment: Alignment.centerLeft,
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: <Widget>[
            Expanded(
              child:Text(_nickName,
                style: KFont.courseTitleStyle,
              ),
            ),
            Expanded(
              child:Text(_phoneNum,
              ),
            )
          ],
        ),
    );
  }

  // 轮播图
  Widget _getImage() {
    List<String> imageList = [KImage.geren_1, KImage.geren_2, KImage.geren_3];
    return Container(
      color: Colors.white,
      height: ScreenUtil().height,
      width: ScreenUtil().width,
      child: Swiper(
        itemBuilder: (BuildContext context, int index){
          return InkWell(
            child: Image.asset("${imageList[index]}", fit: BoxFit.cover,),
            onTap: (){
            },
          );
        },
        itemCount: imageList.length,
        pagination: new SwiperPagination(),
        autoplay: true,
      ),
    );
  }

  _getUserInfo() async{
      request_post_json('userApp/getInfo').then((val){
        var data = json.decode(val.toString());
        setState(() {
          _userInfo = UserAppInfo.fromJson(data['data']);
          _phoneNum = _userInfo.phoneNum;
          _nickName = _userInfo.nickName;
        });
      });
  }
}
